#!/bin/bash
zh_name=`echo $1 | awk -F ':' '{print $1}'`
project_name=`echo $1 | awk -F ':' '{print $2}'`
env_name=$2
start_option=$3
job_name=$4
build_number=$5

base_dir=/data/jenkins/workspace/devops
env_dir=$base_dir/conf/env
jenkins_shell_dir=$base_dir/jenkins/shell
wizbank_env_file=wizbank_env_file.csv
datetime=`date +%F"-"%H":"%M`

zip_name=www.zip
zip_dir=/var/lib/jenkins/jobs/$job_name/builds/$build_number/fileParameters
target_dir=/app/update
get_dir=/data/openresty/wizbank
remote_dir=/data/openresty/remote
wizbank_remote_shell=$remote_dir/wizbank_remote.sh
shell_name=update_"$project_name".sh
remote_shell=$remote_dir/$shell_name

target_ip=`cat $env_dir/$wizbank_env_file    | awk -F ',' -v project_name=$project_name -v env_name=$env_name '{ if ($2 == project_name && $3 == env_name) print $4}'`
code_dir=`cat $env_dir/$wizbank_env_file     | awk -F ',' -v project_name=$project_name -v env_name=$env_name '{ if ($2 == project_name && $3 == env_name) print $5}'`
process_dir=`cat $env_dir/$wizbank_env_file  | awk -F ',' -v project_name=$project_name -v env_name=$env_name '{ if ($2 == project_name && $3 == env_name) print $6}'`
judge_dir=`cat $env_dir/$wizbank_env_file    | awk -F ',' -v project_name=$project_name -v env_name=$env_name '{ if ($2 == project_name && $3 == env_name) print $7}'`

#获取公共函数
#source public_func.sh 
change_ip(){
    net_segment=`echo $target_ip | awk -F '.' '{print $1"."$2"."$3}'`
    login_user=root
    port=xxx
    if [[ $env_name == demo ]];then
        port=xxx
    fi
    if [[ $net_segment == 192.168.2 ]];then
        gossh="ssh -p$port -i /app/pem/nick.pem -o StrictHostKeyChecking=no $login_user"
        get_ip="http://ip:port"
    elif [[  $net_segment == 192.168.4 ]];then
        gossh="ssh -p$port -i /app/pem/nick.pem -o StrictHostKeyChecking=no $login_user"
        get_ip="http://ip:port"   
    elif [[  $net_segment == 192.168.3 ]];then
        gossh="ssh -p$port -i /app/pem/nick.pem -o StrictHostKeyChecking=no -J root@14.29.84.165:9981  $login_user"
        get_ip="http://ip2:port2" 
    elif [[  $project_name == xxx ]] && [[ env_name == prod ]] ;then
        login_user=xxx
        gossh="ssh -p$port -i /app/pem/nick.pem -o StrictHostKeyChecking=no $login_user"
    else
        gossh="ssh -p$port -i /app/pem/nick.pem -o StrictHostKeyChecking=no $login_user"
        get_ip="http://ip2:port2"
    fi
    get_shell=$get_ip/remote    
    get_http="$get_ip/wizbank/$project_name"
}

remote_update(){
    #cd /data/jenkins/workspace/devops
    #git pull origin main
    #传递到web服务器下
    mkdir -p  $get_dir/$project_name
    mv $zip_dir/$zip_name    $get_dir/$project_name/    
    update_shell=/app/update/$shell_name
    update_zipname=/app/update/$zip_name    
    #下载
    #$gossh@$target_ip "mkdir -p /app/update /app/backup;wget $get_http/$zip_name   -q -O  $update_zipname;wget $get_shell/$shell_name -q -O  $update_shell"
    $gossh@$target_ip "mkdir -p /app/update /app/backup;wget $get_http/$zip_name   -q -O  $update_zipname"
    #将默认脚本传递给修改的脚本
    rsync -az $wizbank_remote_shell $remote_shell 
    #修改脚本变量
    sed -i "s#code_dir=xxx#code_dir=$code_dir#g"   $remote_shell
    sed -i "s#start_option=xxx#start_option=$start_option#g"   $remote_shell
    sed -i "s#process_dir=xxx#process_dir=$process_dir#g"   $remote_shell
    $gossh@$target_ip "curl -Ss $get_ip/remote/$shell_name | bash -x"
    #为了安全删除问题
    #rm -f $get_dir/$project_name/$zip_name
    #运行脚本进行更新
    #$gossh@$target_ip "/bin/bash   $update_shell"
}

notice(){    
    if  [[ $? -eq 0  ]];then
        dingding_file=$env_dir/dingding.csv
        notice_file=$jenkins_shell_dir/notice_jenkins.py
        dingding_url=`grep $project_name $dingding_file |  awk -F ',' '{print $2}'`
        if [[ $dingding_url == ""   ]];then
            dingding_url="https://oapi.dingtalk.com/robot/send?access_token=02fd5264b64d3907ec768ad1b133f739cb5720e7e90148d4e6ca37cf34a83a94"
        fi  
        python3  $notice_file   成功   $zh_name  wizbank  $env_name   $build_number $dingding_url
    else
        dingding_file=$env_dir/dingding.csv
        notice_file=$jenkins_shell_dir/notice_jenkins.py
        dingding_url=`grep $project_name $dingding_file |  awk -F ',' '{print $2}'`
        if [[ $dingding_url == ""   ]];then
            dingding_url="https://oapi.dingtalk.com/robot/send?access_token=02fd5264b64d3907ec768ad1b133f739cb5720e7e90148d4e6ca37cf34a83a94"
        fi  
        python3  $notice_file   失败   $zh_name  wizbank  $env_name   $build_number $dingding_url 
    fi
}


if [[ $judge_dir == `unzip -l $zip_dir/$zip_name | awk '{print $NF}' | grep -Ewv 'www.zip|Name|files|--' | head -n 1 |  awk -F '/' '{print $1"/"}'`  ]];then
    change_ip
    remote_update
    notice
else
    echo "请检查压缩包是否合格"
    exit 1
fi

#判断压缩文件是否合格
# if [[ $judge_dir == `unzip -l $zip_dir/$zip_name | awk '{print $NF}' | grep -Ewv 'www.zip|Name|files|--' | head -n 1 |  awk -F '/' '{print $1"/"}'`  ]];then
#     #正式环境只给指定用户指向
#     if [[ $env_name ==  prod ]];then
#         if [[ $jenkins_user ==  manager ]];then
#             change_ip
#             remote_update
#             notice 
#         else
#             echo "非指定用户无法更新生产环境"
#             exit 2
#         fi
#     else
#         change_ip
#         remote_update
#         notice
#     fi
# else
#     echo "请检查压缩包是否合格"
#     exit 1
# fi